package com.ah.shanxi_demo.sync.utils;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import static com.ah.shanxi_demo.sync.utils.Contants.*;

/**
 * 描述:
 * 日期时间工具
 *
 * @author lenghl
 * @create 2018-07-24 18:47
 */
public class DateUtilCustom {
    private DateUtilCustom() {
        throw new IllegalStateException("Utility class");
    }
    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    public static ThreadLocal<DateFormat> df5 = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyMMdd");
        }
    };

    public static ThreadLocal<DateFormat> df4 = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    public static ThreadLocal<DateFormat> df3 = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyMMddHHmm");
        }
    };

    public static Date parse(String dateStr) {
        try {
            return df4.get().parse(dateStr);
        } catch (ParseException e) {
            long mm = NumberUtils.toLong(dateStr, -1);
            if (mm > 0) {
                return new Date(mm);
            }
            logger.error("{} parse faild by  {}", dateStr, e.getMessage());
            return new Date();
        }
    }


    public static String format(Date date, ThreadLocal<DateFormat> df) {
        return df.get().format(date);
    }


    public static String convertTimeToInterval(String time) {
        return convertMsIntervalToMoreUnit(System.currentTimeMillis() - parse(time).getTime());
    }


    public static String convertMsIntervalToMoreUnit(Long interval) {
        if (interval >= MS_PER_YEER) {
            return interval/MS_PER_YEER + "年";
        }
        if (interval >= MS_PER_MONTH) {
            return interval/MS_PER_MONTH + "月";
        }
        if (interval >= MS_PER_WEEK) {
            return interval/MS_PER_WEEK + "周";
        }
        if (interval >= MS_PER_DAY) {
            return interval/MS_PER_DAY + "天";
        }
        if (interval >= MS_PER_HOUR) {
            return interval/MS_PER_HOUR + "小时";
        }
        if (interval >= MS_PER_MINUTE) {
            return interval/MS_PER_MINUTE + "分钟";
        }
        if (interval >= MS_PER_SECOND) {
            return interval/MS_PER_SECOND + "秒";
        }
        return interval + "毫秒";
    }


    public static String convertMsIntervalToDetailDesc(Long interval) {
        StringBuffer s = new StringBuffer();
        s.append((interval/MS_PER_DAY < 10 ? "0" + interval/MS_PER_DAY : interval/MS_PER_DAY) + "天 ");
        interval = interval % MS_PER_DAY;
        s.append((interval/MS_PER_HOUR < 10 ? "0" + interval/MS_PER_HOUR : interval/MS_PER_HOUR) + "小时 ");
        interval = interval % MS_PER_HOUR;
        s.append((interval/MS_PER_MINUTE < 10 ? "0" + interval/MS_PER_MINUTE : interval/MS_PER_MINUTE) + "分钟 ");
        return s.toString();
    }

    /**
     * 把 startTime 向前推(半天 + FIXED_DELAY)的时长.
     * @param startTime
     * @return
     */
    public static String reduceStartTime(String startTime, long fixedDelay){
        Date startDate = DateUtilCustom.parse(startTime);
        if (startDate == null) {
            startDate = new Date();
        }
        return DateUtilCustom.format(new Date(startDate.getTime() - fixedDelay - MS_PER_HALF_DAY), df4);
    }

    public static String dtft1 = "yyyy-MM-dd HH:mm:ss";
    public static DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern(dtft1);
    /** 获取上周最后时间 */
    public static String endTimeInLastWeek() {
        return LocalDateTime.of(LocalDate.now().minusDays(LocalDate.now().getDayOfWeek().getValue())
                , LocalTime.of(23, 59, 59)).format(dtf1);
    }

    /** 获取上周第一天的初始时间 */
    public static String startTimeInLastWeek() {
        return LocalDate.now().minusDays(LocalDate.now().getDayOfWeek().getValue() - 1L + 7L).atStartOfDay().format(dtf1);
    }

    /** 获取当上月最后时间 */
    public static String endTimeInLastMonth() {
        return LocalDateTime.of(LocalDate.now().minusDays(LocalDate.now().getDayOfMonth())
                , LocalTime.of(23, 59, 59)).format(dtf1);
    }

    /** 获取上月第一天的初始时间 */
    public static String startTimeInLastMonth() {
        LocalDate lastDay = LocalDate.now().minusDays(LocalDate.now().getDayOfMonth());
        return lastDay.minusDays(lastDay.getDayOfMonth() - 1L).atStartOfDay().format(dtf1);
    }
}
